﻿@using Presentation.Web.Admin
@{
    ViewBag.Title = "推送管理";
    ViewBag.subtitle = "推送管理";
}
@using (Ajax.BeginForm("index", "push", new AjaxOptions
{
    HttpMethod = "post",
    OnBegin = "AjaxOptionPushBegin",
    OnFailure = "AjaxOptionPustFailure",
    OnSuccess = "AjaxOptionPushSuccess"
}))
{ 
    @Html.Partial("_PushCustomSetting")
    @Html.Partial("_PushForm")
}
<script type="text/javascript">
    $(function () {
        $('#repush').hide();
        ResetContent();
        $('#contents').keyup(function () {
            ResetContent();
        });
        $('#repush').click(function () {
            var data = { date: new Date(),
                sleepBetweenNote: $('#sleepBetweenNote').val(),
                connNum: $('#sleepBetweenNote').val(),
                sendRetry: $('#sleepBetweenNote').val(),
                reconnectDelay: $('#sleepBetweenNote').val(),
                badge: $('#sleepBetweenNote').val()
            };
            $.post('/push/reindex', data, function (res) {
                AjaxOptionPushSuccess(res);
            }, 'json');
        });
    });

    function ResetContent() {
        var allowMaxLength = 256;
        var left = allowMaxLength;
        var targetLength = $('#contents').val().length;
        if (targetLength > allowMaxLength) {
            left = 0;
            var con = $('#contents').val();
            $('#contents').val(con.substring(0, allowMaxLength));
        }
        else {
            left = allowMaxLength - targetLength;
        }
        $('#leftNum').html(left);
    }

    function AjaxOptionPushBegin() {
        var con = $('#contents');
        if (con.val() == '') {
            con.focus();
            alert('推送内容不能为空');
            return false;
        }
        if (con.val().length > 256) {
            alert('推送的内容不能超过256个字');
            return false;
        }

        var sendRetry = $('#sendRetry');
        if (sendRetry.val() == '' || sendRetry.val() < 0 || sendRetry.val() > 10) {
            sendRetry.focus();
            alert('请填写正确的重试次数');
            return false;
        }
        var connNum = $('#connNum');
        if (connNum.val() == '' || connNum.val() < 0 || connNum.val() > 50) {
            connNum.focus();
            alert('请填写正确的推送连接数');
            return false;
        }
        var sleepBetweenNote = $('#sleepBetweenNote');
        if (sleepBetweenNote.val() == '' || sleepBetweenNote.val() <= 0) {
            sleepBetweenNote.focus();
            alert('请填写正确的客户端推送时间间隔,提示该时间必须大于0');
            return false;
        }
        var reconnectDelay = $('#reconnectDelay');
        if (reconnectDelay.val() == '' || reconnectDelay.val() < 0) {
            reconnectDelay.focus();
            alert('请填写正确的推送成功后重新连接延迟时间');
            return false;
        }
        $('input[type="submit"]').attr('disabled', 'disabled');
    }

    function AjaxOptionPustFailure() {
        alert('由于网络或其他原因，推送失败了或部分推送失败了，详情请参见推送日志。');
        $('input[type="submit"]').removeAttr('disabled');
    }

    function AjaxOptionPushSuccess(res) {
        if (res != null) {
            var str = (res.left > 0 ? ('部分客户端推送成功。\r\n其中有 ' + res.left + ' 个推送失败\r\n你可以点击下方的重新推送\r\n来发送信息到失败的客户端') : '推送成功');
            if (res.left > 0) {
                $('#repush').show();
            }
            alert(str);
        }
        $('input[type="submit"]').removeAttr('disabled');
    }
</script>
